package com.java.controller;

import com.java.bean.BootstrapTableCourier;
import com.java.bean.Courier;
import com.java.bean.Message;
import com.java.bean.ResultData;
import com.java.exception.DuplicateException;
import com.java.mvc.Body;
import com.java.service.CourierService;
import com.java.exception.ExceptionStateEnum;
import com.java.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
public class CourierController {

//    @Body("/admin/views/courier/.do")
//    public String (HttpServletRequest req, HttpServletResponse resp) {

    @Body("/admin/views/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            final int id = Integer.parseInt(req.getParameter("id"));
            if (CourierService.delete(id)) {
                return JSONUtil.toJSON(new Message(0, "删除成功"));
            }
        } catch (NumberFormatException ignored) {
        }
        return JSONUtil.toJSON(new Message(-1, "删除失败"));
    }

    @Body("/admin/views/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        final int result;
        try {
            final int id = Integer.parseInt(req.getParameter("id"));
            final String name = req.getParameter("name");
            final String phone = req.getParameter("phone");
            final String idNumber = req.getParameter("idNumber");
            final String email = req.getParameter("email");
            final String addr = req.getParameter("addr");
            final String passwd = req.getParameter("passwd");
            final int dispatches = Integer.parseInt(req.getParameter("dispatches"));
            final int status = Integer.parseInt(req.getParameter("status"));
            final Courier courier = new Courier(name, phone, idNumber, passwd, status, dispatches, null, email, addr);
            result = CourierService.update(id, courier);
        } catch (NumberFormatException ignored) {
            return JSONUtil.toJSON(new Message(-1, "更改快递员失败"));
        } catch (DuplicateException e) {
            return JSONUtil.toJSON(new Message(e.getType().getVal(), getMessage(e.getType())));
        }
        return JSONUtil.toJSON(new Message(result, result == -1 ? "更改快递员失败" : "更改快递员成功"));
    }

    private String getMessage(ExceptionStateEnum e) {
        switch (e) {
            case DUPLICATE_PHONE:
                return "电话号码已被注册";
            case DUPLICATE_IDENTIFICATION:
                return "身份证号码已被注册";
            case DUPLICATE_EMAIL:
                return "电子邮箱地址已被注册";
            default:
                return "操作失败";
        }
    }

    @Body("/admin/views/courier/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp) {
        final String phone = req.getParameter("phone");
        final Courier courier = CourierService.getCourierByPhone(phone);
        if (courier != null) {
            return JSONUtil.toJSON(new Message(0, "查询成功", new BootstrapTableCourier(courier)));
        }
        return JSONUtil.toJSON(new Message(-1, "该手机号尚未注册"));
    }

    @Body("/admin/views/courier/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        final String name = req.getParameter("name");
        final String phone = req.getParameter("phone");
        final String idNumber = req.getParameter("idNumber");
        final String email = req.getParameter("email");
        final String passwd = req.getParameter("passwd");
        final String addr = req.getParameter("addr");
        final Courier courier = new Courier(name, phone, idNumber, passwd, new Timestamp(new Date().getTime()), email, addr);
        final int status;
        try {
            status = CourierService.insert(courier);
        } catch (DuplicateException e) {
            return JSONUtil.toJSON(new Message(e.getType().getVal(), getMessage(e.getType())));
        }
        return JSONUtil.toJSON(new Message(status, status == -1 ? "新增快递员失败" : "新增快递员成功"));
    }


    @Body("/admin/views/courier/list.do")
    public String list(HttpServletRequest req   , HttpServletResponse resp) {
        final int offset = Integer.parseInt(req.getParameter("offset"));
        final int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        Integer total = CourierService.console().get("size");
        final List<Courier> couriers;
        if (total > offset)
            couriers = CourierService.listCourier(offset, pageNumber);
        else
            couriers = CourierService.listCourier(Math.max(offset - pageNumber, 0), pageNumber);
        if (couriers != null) {
            final List<BootstrapTableCourier> rows = new ArrayList<>();
            for (Courier courier : couriers) {
                rows.add(new BootstrapTableCourier(courier));
            }
            ResultData<BootstrapTableCourier> data = new ResultData<>();
            data.setRows(rows);
            data.setTotal(total);
            return JSONUtil.toJSON(data);
        }
        return null;
    }

}
